package com.pandora.component;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import com.pandora.annotation.NameMapping;
import com.pandora.util.StringUtil;

public class DomainHelper {
	private final String defaultName="未命名"; 
	public DomainTree extractDomainTree(Class<?> clazz) {
		TreeNode root=createTreeNode(clazz,null,null,null,null);
		DomainTree domainTree=new DomainTree(root);
		return domainTree;
	}
	public TreeNode createTreeNode(Class<?> clazz,String englishName,String chineseName,String superLevel,String propertyName) {
		TreeNode treeNode =new TreeNode();
		String className=clazz.getName();
		String level=null;
		if (StringUtil.isBlank(englishName)||StringUtil.isBlank(chineseName)) {
			if (clazz.isAnnotationPresent(NameMapping.class)) {
				NameMapping nameMappingAnnotation=clazz.getAnnotation(NameMapping.class);
				englishName=nameMappingAnnotation.englishName();
				chineseName=nameMappingAnnotation.chineseName();
			}else {
				englishName=clazz.getSimpleName();
				chineseName=defaultName;
			}
		}
		if (StringUtil.isNotBlank(superLevel)) {
			level=superLevel+"."+englishName;
		}else {
			level=clazz.getSimpleName();
		}
		
		treeNode.setPropertyName(propertyName);
		treeNode.setEnglishName(englishName);
		treeNode.setChineseName(chineseName);
		treeNode.setClassName(className);
		treeNode.setLevel(level);
		if (!className.startsWith("java.lang")) {
			BeanInfo beanInfo=null;
			try {
				beanInfo = Introspector.getBeanInfo(clazz,clazz.getSuperclass());
			} catch (IntrospectionException e) {
				e.printStackTrace();
			}
			List<TreeNode> treeNodeChildren=new ArrayList<TreeNode>();
			for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
				Class<?> propClass=pd.getPropertyType();
				String propEnglishName=null;
				String propChineseName=null;
				Method readMethod=pd.getReadMethod();
				if (readMethod.isAnnotationPresent(NameMapping.class)) {
					NameMapping nameMappingAnnotation=readMethod.getAnnotation(NameMapping.class);
					propEnglishName=nameMappingAnnotation.englishName();
					propChineseName=nameMappingAnnotation.chineseName();
				}else {
					Field field;
					try {
						field = clazz.getDeclaredField(pd.getName());
						field.setAccessible(false);
						if (field.isAnnotationPresent(NameMapping.class)) {
							NameMapping nameMappingAnnotation=field.getAnnotation(NameMapping.class);
							propEnglishName=nameMappingAnnotation.englishName();
							propChineseName=nameMappingAnnotation.chineseName();
						}else {
							propEnglishName=pd.getName();
							propChineseName=defaultName;
						}
					} catch (Exception e) {
						//如果根据属性名获取不到同名Field就放弃获取该名字段上的注解采用类名为englishName,“未命名”为chineseName
						propEnglishName=pd.getName();
						propChineseName=defaultName;
					}
				}
				TreeNode childTreeNode=createTreeNode(propClass,propEnglishName,propChineseName,level,pd.getName());
				treeNodeChildren.add(childTreeNode);
			}
			treeNode.setChildren(treeNodeChildren);
		}
		
		return treeNode;
	}
}